import request from "@/utils/request";
import type { LoginRequest, RegisterRequest, AuthResponse, ApiResponse } from "@/types";

// 用户登录
export function login(data: LoginRequest): Promise<AuthResponse> {
  return request({
    url: "/auth/login",
    method: "post",
    data,
  });
}

// 用户注册
export function register(data: RegisterRequest): Promise<ApiResponse> {
  return request({
    url: "/auth/register",
    method: "post",
    data,
  });
}

// 刷新token
export function refreshToken(refreshToken: string): Promise<AuthResponse> {
  return request({
    url: "/auth/refresh",
    method: "post",
    data: { refreshToken },
  });
}

// 用户登出
export function logout(): Promise<ApiResponse> {
  return request({
    url: "/auth/logout",
    method: "post",
  });
}

// 获取当前用户信息
export function getUserInfo(): Promise<AuthResponse["userInfo"]> {
  return request({
    url: "/auth/me",
    method: "get",
  });
}

// 修改密码
export function changePassword(data: {
  oldPassword: string;
  newPassword: string;
  confirmPassword: string;
}): Promise<ApiResponse> {
  return request({
    url: "/auth/change-password",
    method: "post",
    data,
  });
}

// 检查用户名是否可用
export function checkUsername(username: string): Promise<ApiResponse<boolean>> {
  return request({
    url: "/auth/check-username",
    method: "get",
    params: { username },
  });
}

// 检查邮箱是否可用
export function checkEmail(email: string): Promise<ApiResponse<boolean>> {
  return request({
    url: "/auth/check-email",
    method: "get",
    params: { email },
  });
}

// 发送重置密码邮件
export function sendResetPasswordEmail(email: string): Promise<ApiResponse> {
  return request({
    url: "/auth/reset-password",
    method: "post",
    data: { email },
  });
}

// 重置密码
export function resetPassword(data: {
  token: string;
  newPassword: string;
  confirmPassword: string;
}): Promise<ApiResponse> {
  return request({
    url: "/auth/reset-password/confirm",
    method: "post",
    data,
  });
}
